package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.security.Key;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {
    @Autowired
    private StringRedisTemplate template;


    @Override
    public void sendVerifyCode(String phone) {
        //String key = "sendVerifyCode_phone:"+phone;
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        System.out.println("发送的验证码: " + code);
        template.opsForValue().set(
                RedisKeys.VERIY_CODE.join(phone)
                , code
                , RedisKeys.VERIY_CODE.getTime()
                , TimeUnit.SECONDS);

        //RestTemplate restTemplate = new RestTemplate();
        //System.out.println("phone: " + phone);
        //String url = "https://way.jd.com/kaixintong/kaixintong?mobile={1}&content=【凯信通】您的验证码是：{2}&appkey={3}";
        //
        //System.out.println(url);
        //String ret = restTemplate.getForObject(url, String.class, phone, code,"f6342c2f6de163ca402a683bc3ca5a22");
        ////String ret = restTemplate.getForObject(url, String.class);
        //System.out.println(ret);
        //if(!ret.contains("ok")){
        //    throw new LogicException("验证码请求失败");
        //}
    }

    @Override
    public String getVerifyCode(String phone) {
        //String key = "sendVerifyCode_phone:"+phone;
        System.out.println(RedisKeys.VERIY_CODE.join(phone));
        String code = template.opsForValue().get(RedisKeys.VERIY_CODE.join(phone));
        System.out.println("拿到的验证码: " + code);
        return code;
    }

    @Override
    public UserInfo getUserByToken(String token) {
        //  判断用户是否为空
        if (!StringUtils.hasLength(token)) {
            return null;
        }
        // 拼接key
        String key = RedisKeys.USER_LOGIN_TOKEN.join(token);
        // 判断用户是否已经过期
        if (template.hasKey(key)){
            String user = template.opsForValue().get(key);
            UserInfo userInfo = JSON.parseObject(user, UserInfo.class);
            // 重置token的有效时间
            template.expire(key, RedisKeys.USER_LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
            return userInfo;
        }
        return null;
    }

    @Override
    public String setUserByToken(UserInfo userInfo) {
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        String user = JSON.toJSONString(userInfo);
        String token = RedisKeys.USER_LOGIN_TOKEN.join(key);
        template.opsForValue().set(token, user, RedisKeys.USER_LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
        return key;
    }
}
